import {
	config
} from "./config.js"
const Http = {
	async request({
		url,
		data = {},
		method = "GET"
	}) {
		// 添加拦截器
		uni.addInterceptor('request', {
			invoke(args) {
				const userInfo = uni.getStorageSync("userinfo")
				if (userInfo.token) {
					args.header.ba_user_token = userInfo.token || ''
					args.header.bausertoken = userInfo.token || ''
				} else {
					const tmtoken = uni.getStorageSync('token') || '';
					if (tmtoken) {
						args.header.ba_user_token = tmtoken
						args.header.bausertoken = tmtoken
					}
				}
			},
			success(succ) {
				// console.log("succ: ", succ);
			},
			fail(err) {
				console.log("err: ", err);
			},
			complete(complete) {}
		})
		data.server=1
		let apiurl=config.apiBaseUrl
		if (process.env.NODE_ENV === 'development') {
			apiurl=config.devUrl
		} else {
			apiurl=config.apiBaseUrl
		}
		const result = await uni.request({
			method,
			url: `${apiurl}${url}`,
			data,
			header: {
				"content-type": "application/x-www-form-urlencoded",
			}
		});
		if (!result) {
			return
		}
		let res = result.data
		if (res.code == 1) {
			return Promise.resolve(res)
		} else {
			console.log(res,'<><>')
			if(res.code==303){uni.clearStorageSync();}
			if(res.code==303){
				uni.clearStorageSync();
				uni.reLaunch({
					url: '/pages/login/login'
				});
				return false
			}
			uni.showToast({
				title: res.msg || "未知错误",
				icon: "error",
				duration: 4000
			})
			return Promise.reject(res)
		}
	}
}
export {
	Http
}